package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.Upkeep;
import com.sailfish.springbootdemo.pojo.db1.UpkeepAudit;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface UpkeepAuditDao {

    /**
     * 根据设备资产查询 保养计划
     *
     * @param assetNumber
     * @return
     */
    @Results(id = "upkeepAuditResultMap", value = {
            @Result(column = "upkeep_id", property = "upkeepId", jdbcType = JdbcType.INTEGER),
            @Result(column = "upkeep_type", property = "upkeepType", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_id", property = "pcbaDeviceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "asset_number", property = "assetNumber", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "first_upkeep_time", property = "firstUpkeepTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "owner_id", property = "ownerId", jdbcType = JdbcType.INTEGER),
            @Result(column = "contact_method", property = "contactMethod", jdbcType = JdbcType.VARCHAR),
            @Result(column = "upkeep_status", property = "upkeepStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "version", property = "version", jdbcType = JdbcType.INTEGER),
            @Result(column = "audit_id", property = "auditId", jdbcType = JdbcType.INTEGER),
            @Result(column = "notes", property = "notes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_delete", property = "isDelete", jdbcType = JdbcType.INTEGER),
            @Result(column = "owner_name", property = "ownerName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "submitter", property = "submitter", jdbcType = JdbcType.INTEGER),
            @Result(column = "auditors", property = "auditors", jdbcType = JdbcType.VARCHAR),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.UPKEEP_AUDIT + " where 1 = 1 " +
            "<if test='assetNumber != null and assetNumber != \"\" '>" +
            " and asset_number = #{assetNumber}" +
            "</if>" +
            "<if test='upkeepType != null and upkeepType != \"\" '>" +
            " and upkeep_type = #{upkeepType}" +
            "</if>" +
            "<if test=\"isDeleteList != null and isDeleteList.size() > 0 \">\n" +
            "                and is_delete in\n" +
            "                <foreach item=\"item\" index=\"index\" collection=\"isDeleteList\"\n" +
            "                         open=\"(\" separator=\",\" close=\")\">\n" +
            "                    #{item}\n" +
            "                </foreach>\n" +
            "            </if>\n" +
            " order by upkeep_type" +
            "</script>")
    List<UpkeepAudit> getUpkeepAuditByDeviceId(@Param("assetNumber") String assetNumber, @Param("upkeepType") Integer upkeepType, @Param("isDeleteList") List<Integer> isDeleteList);

    /**
     * 根据资产和保养计划类型查询
     *
     * @param assetNumber
     * @param upkeepType
     * @return
     */
    @Select("select * from " + DB1TableName.UPKEEP_AUDIT + " where asset_number = #{assetNumber} and upkeep_type = #{upkeepType}")
    @ResultMap(value = "upkeepAuditResultMap")
    UpkeepAudit getUpkeepAuditByAssetNumberAndUpkeepType(@Param("assetNumber") String assetNumber, @Param("upkeepType") Integer upkeepType);

    /**
     * 判断该资产编号的设备是否存在保养计划
     *
     * @param assetNumber
     * @param upkeepId
     * @return
     */
    @Select("select * from " + DB1TableName.UPKEEP_AUDIT + " where asset_number = #{assetNumber} and upkeep_id != #{upkeepId}")
    @ResultMap(value = "upkeepAuditResultMap")
    List<UpkeepAudit> judgeExistUpkeepAuditByAssetNumber(@Param("assetNumber") String assetNumber, @Param("upkeepId") Integer upkeepId);

    /**
     * 根据id查询
     *
     * @param upkeepId
     * @return
     */
    @ResultMap(value = "upkeepAuditResultMap")
    @Select("select * from " + DB1TableName.UPKEEP_AUDIT + " where upkeep_id = #{upkeepId}")
    UpkeepAudit getById(@Param("upkeepId") Integer upkeepId);

    /**
     * 新增
     *
     * @param upkeepAudit
     * @return
     */
    @Insert("insert into " + DB1TableName.UPKEEP_AUDIT + "(upkeep_type,asset_number,pcba_device_id,create_time,first_upkeep_time,owner_id,contact_method,version,audit_id,notes,is_delete,upkeep_status,owner_name,submitter,auditors) values " +
            "(#{upkeepAudit.upkeepType},#{upkeepAudit.assetNumber},#{upkeepAudit.pcbaDeviceId},#{upkeepAudit.createTime},#{upkeepAudit.firstUpkeepTime},#{upkeepAudit.ownerId},#{upkeepAudit.contactMethod},#{upkeepAudit.version},#{upkeepAudit.auditId},#{upkeepAudit.notes},#{upkeepAudit.isDelete},#{upkeepAudit.upkeepStatus},#{upkeepAudit.ownerName},#{upkeepAudit.submitter},#{upkeepAudit.auditors})")
    Integer addUpkeepAudit(@Param("upkeepAudit") UpkeepAudit upkeepAudit);

    /**
     * 更新
     *
     * @param upkeepAudit
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.UPKEEP_AUDIT +
            "<set>" +
            "<if test='upkeepAudit.firstUpkeepTime != null '>" +
            " first_upkeep_time = #{upkeepAudit.firstUpkeepTime}," +
            "</if>" +
            "<if test='upkeepAudit.ownerId != null and upkeepAudit.ownerId != \"\" '>" +
            " owner_id = #{upkeepAudit.ownerId}," +
            "</if>" +
            "<if test='upkeepAudit.contactMethod != null and upkeepAudit.contactMethod != \"\" '>" +
            " contact_method = #{upkeepAudit.contactMethod}," +
            "</if>" +
            "<if test='upkeepAudit.version != null and upkeepAudit.version != \"\" '>" +
            " version = #{upkeepAudit.version}," +
            "</if>" +
            "<if test='upkeepAudit.auditId != null and upkeepAudit.auditId != \"\" '>" +
            " audit_id = #{upkeepAudit.auditId}," +
            "</if>" +
            "<if test='upkeepAudit.notes != null and upkeepAudit.notes != \"\" '>" +
            " notes = #{upkeepAudit.notes}," +
            "</if>" +
            "<if test='upkeepAudit.upkeepStatus != null and upkeepAudit.upkeepStatus != \"\" '>" +
            " upkeep_status = #{upkeepAudit.upkeepStatus}," +
            "</if>" +
            "<if test='upkeepAudit.isDelete != null and upkeepAudit.isDelete != \"\" '>" +
            " is_delete = #{upkeepAudit.isDelete}," +
            "</if>" +
            "<if test='upkeepAudit.ownerName != null and upkeepAudit.ownerName != \"\" '>" +
            " owner_name = #{upkeepAudit.ownerName}," +
            "</if>" +
            "<if test='upkeepAudit.updateTime != null '>" +
            " update_time = #{upkeepAudit.updateTime}," +
            "</if>" +
            "<if test='upkeepAudit.submitter != null and upkeepAudit.submitter != \"\" '>" +
            " submitter = #{upkeepAudit.submitter}," +
            "</if>" +
            "<if test='upkeepAudit.auditors != null and upkeepAudit.auditors != \"\" '>" +
            " auditors = #{upkeepAudit.auditors}," +
            "</if>" +
            "</set>" +
            " where upkeep_id = #{upkeepAudit.upkeepId}" +
            "</script>")
    Integer updateUpkeepAudit(@Param("upkeepAudit") UpkeepAudit upkeepAudit);

    /**
     * 删除
     *
     * @param upkeepId
     * @return
     */
    @Delete("delete from " + DB1TableName.UPKEEP_AUDIT + " where upkeep_id = #{upkeepId}")
    Integer deleteUpkeepAuditById(@Param("upkeepId") Integer upkeepId);
}
